﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace wlm.wsx.src.wlm.Newwsx
{
    public abstract class OperatorBase
    {
        private string _OperatorSymbol = string.Empty;
        /// <summary>
        /// 运算符符号
        /// </summary>
        public string OperatorSymbol 
        {
            get{return this._OperatorSymbol;}
        }

       
        /// <summary>
        /// 计算结果
        /// </summary>
        /// <param name="operands">需要的操作数（已检查）</param>
        /// <returns>返回计算结果</returns>
        public abstract double OnCalculate(double[] operands);

       
        public static OperatorBase CreateOperatorBase(string exp)
        {
            OperatorBase operatorBase = null;
            switch (exp.ToUpper())
            {
                 case "+":
                    operatorBase = new OperatorPlus();
                    break;
                 case "++":
                    operatorBase = new OperatorPlusPlus();
                    break;
                case "-":
                    operatorBase = new OperatorMinus();
                    break;
                case "--":
                    operatorBase = new OperatorMinusMinus();
                    break;
                case "*":
                    operatorBase = new OperatorMultiply();
                    break;
                case "/":
                    operatorBase = new OperatorDivide();
                    break;
                case "GT":
                case "LT":
                case "GE":
                case "LE":
                case "EQ":
                    operatorBase = new OperatorCompare();
                    break;
                case "OR":
                    operatorBase = new OperatorBitOr();
                    break;
                case "AND":
                    operatorBase = new OperatorBitAnd();
                    break;
            }
            operatorBase._OperatorSymbol = exp.ToUpper();
            return operatorBase;
        }
        /// <summary>
        /// 连接字符串
        /// </summary>
        /// <param name="operands"></param>
        /// <returns></returns>
        public virtual string OnCalculate(string[] operands)
        {
            if (operands == null || operands.Length != 2)
                return string.Empty;

            StringBuilder sb = new StringBuilder();
            foreach (string strTxt in operands)
                sb.Append(strTxt);

            return sb.ToString();
        }

        /// <summary>
        /// 条件比较
        /// </summary>
        /// <param name="operands">参数集合</param>
        /// <returns></returns>
        public virtual bool OnCalculate(int[] operands)
        {
            if (operands == null || operands.Length != 2)
                return false;

            return operands[0].Equals(operands[1]);
        }
        
    }
    
}
